package org.budo.canal.meta;

import java.util.Map;

import org.budo.support.lang.util.ReflectUtil;

import com.alibaba.otter.canal.meta.PeriodMixedMetaManager;
import com.alibaba.otter.canal.meta.exception.CanalMetaManagerException;
import com.alibaba.otter.canal.protocol.ClientIdentity;
import com.alibaba.otter.canal.protocol.position.PositionRange;

import lombok.extern.slf4j.Slf4j;

/**
 * 没理解清这个的意义,如果使用内存版这个就可以不存在了
 * 
 * @author lmw
 */
@Slf4j
public class BudoPeriodMixedMetaManager extends PeriodMixedMetaManager {
    /**
     * @see com.alibaba.otter.canal.meta.MemoryMetaManager.MemoryClientIdentityBatch#removePositionRange(Long)
     */
    @Override
    public PositionRange removeBatch(ClientIdentity clientIdentity, Long batchId) throws CanalMetaManagerException {
        try {
            return super.removeBatch(clientIdentity, batchId);
        } catch (Throwable e) {
            log.error("#20 clientIdentity=" + clientIdentity + ", batchId=" + batchId + ", e=" + e, e);

            MemoryClientIdentityBatch memoryClientIdentityBatch = this.batches.get(clientIdentity);
            Map<Long, PositionRange> batches = (Map<Long, PositionRange>) ReflectUtil.getFieldValue(MemoryClientIdentityBatch.class, "batches", memoryClientIdentityBatch);
            return batches.remove(batchId);
        }
    }
}